﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;

namespace Zor_DAL
{
    public class PropertyInfo_DAL
    {
        /// <summary>
        /// 房产信息添加
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        public static BaseAnswer InserPropertyInfo(PropertyInfo pi)
        {
            var sql = @"insert into PropertyInfo(Dt_ParentName,Dt_ParentId,Dt_Name,Dt_Id,Bd_Name,Bd_Id,Uim_Name,Uim_Id,Hs_Number,Hs_Id,
                        AuditStatus,AuditMeg,Address,UserCardId,UserName) 
                        values(@Dt_ParentName,@Dt_ParentId,@Dt_Name,@Dt_Id,@Bd_Name,@Bd_Id,@Uim_Name,@Uim_Id,@Hs_Number,@Hs_Id,
                        @AuditStatus,@AuditMeg,@Address,@UserCardId,@UserName)";
            BaseAnswer bar = DapperInsert<PropertyInfo>.DoInsert(sql, pi);
            return bar;
        }

        /// <summary>
        /// 获取房产信息
        /// </summary>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        public static List<PropertyInfo> GetPropertyInfoList(string sqlWhere)
        {
            var sql = "select * from PropertyInfo where 1=1 ";
            if (!string.IsNullOrEmpty(sqlWhere))
            {
                sql += sqlWhere;
            }
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList(sql, null);
            return lst;
        }

        /// <summary>
        /// 获取街办信息
        /// </summary>
        /// <returns>返回Dt_ParentId街办ID，Dt_ParentName街办名称</returns>
        public static List<PropertyInfo> GetStreetOffice() 
        {
            var sql = @"select Dt_Id Dt_ParentId, Dt_Name AS Dt_ParentName from Sys_Department where Dt_Level = 3 ";
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList("1",sql, null);
            return lst;
        }

        /// <summary>
        /// 根据街办ID获取社区信息
        /// </summary>
        /// <param name="parentId">街办ID</param>
        /// <returns>返回Dt_Id社区ID,Dt_Name社区名称</returns>
        public static List<PropertyInfo> GetCommunityByStreetOfficeID(int parentId) 
        {
            var sql = @"select Dt_Id, Dt_Name from Sys_Department ";
            if (parentId > 0)
            {
                sql += " where Dt_ParentId = " + parentId;
            }
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList("1", sql, null);
            return lst;
        }

        /// <summary>
        /// 根据社区ID获取楼宇信息
        /// </summary>
        /// <param name="communityID">社区ID</param>
        /// <returns>返回Bd_Id楼宇ID,Bd_Name楼宇名称</returns>
        public static List<PropertyInfo> GetBuildingByCommunityID(int communityID)
        {
            var sql = @" select Bd_Id,Bd_Name from Zor_Building  ";
            if (communityID > 0)
            {
                sql += " where Dt_Id = " + communityID;
            }
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList("1", sql, null);
            return lst;
        }

        /// <summary>
        /// 根据楼宇ID获取单元信息
        /// </summary>
        /// <param name="buildingID">楼宇ID</param>
        /// <returns>返回Uim_Id单元ID,Uim_Name单元名称</returns>
        public static List<PropertyInfo> GetUnitInforByBuildingID(int buildingID)
        {
            var sql = @" select Uim_Id,Uim_Name from Zor_UnitInfor  ";
            if (buildingID > 0)
            {
                sql += " where BD_Id = " + buildingID;
            }
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList("1", sql, null);
            return lst;
        }

        /// <summary>
        /// 根据单元ID获取房屋信息
        /// </summary>
        /// <param name="unitInforID">单元ID</param>
        /// <returns>返回Hs_ID房屋ID,Hs_Number房屋名称</returns>
        public static List<PropertyInfo> GetHouseByUnitInforID(int unitInforID)
        {
            var sql = @" select Hs_ID,Hs_Number from Zor_Hous  ";
            if (unitInforID > 0)
            {
                sql += " where Uf_Id = " + unitInforID;
            }
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList("1", sql, null);
            return lst;
        }

        /// <summary>
        /// 获取推送消息集合，根据用户权限
        /// </summary>
        /// <returns></returns>
        public static List<PropertyInfo> GetPushMegList(PropertyInfo pi)
        {
            List<PropertyInfo> lst = new List<PropertyInfo>();
            var sql = @" select pro.*,us.User_RealName as AuditUserName from [dbo].[PropertyInfo] pro
                         left join Users us on pro.AuditUserId=us.User_Id ";
            if (pi.Dt_ParentId != 0)
            {
                sql += " where pro.Dt_ParentId=@Dt_ParentId ";
                lst = DapperQuery<PropertyInfo>.DoQueryList(sql, pi);
            }
            else
            {
                lst = DapperQuery<PropertyInfo>.DoQueryList(sql, null);
            }
            return lst;
        }

        /// <summary>
        /// 获取推送消息集合，根据用户权限
        /// </summary>
        /// <returns></returns>
        public static List<PropertyInfo> GetPushMegListByUserId(PropertyInfo pi)
        {
            var sql = @"select * from [dbo].[PropertyInfo] where UserCardId=@UserCardId and AuditStatus=2 or AuditStatus=0";
            List<PropertyInfo> lst = DapperQuery<PropertyInfo>.DoQueryList(sql, pi);
            return lst;
        }

        /// <summary>
        /// 删除推送消息
        /// </summary>
        /// <param name="ids">id集合</param>
        /// <returns></returns>
        public static BaseAnswer DelPushMeg(int[] ids) 
        {
            var sql = @"delete from PropertyInfo where  Property_Id in @Property_Id";
            BaseAnswer bar = DapperDelete<Users>.DoDelete(sql, new { Property_Id = ids });
            return bar;
        }

        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="pi"></param>
        /// <returns></returns>
        public static BaseAnswer ApplyPushMeg(PropertyInfo pi)
        {
            var sql = @"update PropertyInfo set AuditStatus = @AuditStatus,AuditMeg = @AuditMeg,AuditUserId=@AuditUserId,AuditTime=@AuditTime where Property_Id = @Property_Id";
            BaseAnswer bar = DapperEdit<FlowInfo>.DoEdit(sql, pi);
            return bar;
        }


        /// <summary>
        /// 获取房屋信息
        /// </summary>
        /// <param name="ri"></param>
        /// <returns></returns>
        public static List<PropertyInfo> GetPropertyInfoListByHsId(PropertyInfo pi)
        {
            List<PropertyInfo> lst = new List<PropertyInfo>();
            var sql = "select Dt_ParentName,Dt_ParentId,Dt_Name,Dt_Id,Bd_Name,Bd_Id,Uim_Name,Uim_Id,Hs_Number,Hs_Id from V_GetTaxRecord where Hs_Id=@Hs_Id";
            lst = DapperQuery<PropertyInfo>.DoQueryList("1", sql, pi);
            return lst;
        }


        /// <summary>
        /// 获取房屋信息
        /// </summary>
        /// <param name="ri"></param>
        /// <returns></returns>
        public static List<PropertyInfo> GetPropertyInfoListByDtParentId(PropertyInfo pi)
        {
            List<PropertyInfo> lst = new List<PropertyInfo>();
            var sql = "select Dt_ParentName,Dt_ParentId,Dt_Name,Dt_Id,Bd_Name,Bd_Id,Uim_Name,Uim_Id,Hs_Number,Hs_Id from V_GetTaxRecord";
            if (pi.Dt_ParentId!=0)
            {
                sql += " where Dt_ParentId=@Dt_ParentId ";
            }
            lst = DapperQuery<PropertyInfo>.DoQueryList("1", sql, pi);
            return lst;
        }
    }
}
